﻿/*!
 *
 * Date: 10/05/2011
 * jQ-comerce javaScript plugin 
 * http://pukis.com.br/
 *
 * Copyright 2011, Felipe Assunção //felipeilllustrator@gmail.com //@felipeuntill
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 *
 * Require jQuery JavaScript Library v1.4 or Newest
 * http://jquery.com/
 * Copyright 2011, John Resig
 * Released under the MIT and GPL Licenses.
 * 
 */
$(function() {
    // Begging the plugin resposable of make the select UI.
    $.fn.jQSelect = (function(options) {
        
		// Defining all customs variables.
        var options = $.extend({
            selectInstance : $(this),
            optionList : $(this).find("option"),
            uiContainer : null,
            uiChild : null,
            uiItems : null,
			rotulo : "Selecione",
            callfun : (function() {
            
            }),
            selectfirst : true
        },options);
        
        $(this).each(function() {
        // Creating UI the element.
        $(this).before('<div class="jQcustomselect" ><a title="Clique aqui para selecionar uma op&ccedil;&atilde;o" class="jqselectlabel" href="javascript:void(0)">' + options.rotulo + '</a></div>');
        // Matching the added object.
        options.uiContainer = $(this).prev();
        // Creating UI inside the element.
        $(options.uiContainer).append('<ul class="ui jQSelect" > </ul>')
        // Matching the added object.
        options.uiChild = $(options.uiContainer).find(".jQSelect");
        
        // Travelsing all option's.
        $(options.optionList).each(function() {
            // Appending the option content on a ul created previously.
            $(options.uiChild).append('<li><a href="javascript:void(0)">' + $(this).html() + '</a></li>');
        });
        
        // Matching the added items.
        options.uiItems = $(options.uiContainer).find("li");
        
        // When the list item has clicked.
        $(options.uiItems).click(function() {
            // Setting up the value to the original select element.
            $(options.selectInstance).val($(options.optionList[ $(this).index($(options.optionList).selection) ]).val());
			// Setting up the select texto to the label.
			$(options.uiContainer).children('.jqselectlabel').html($(options.optionList[ $(this).index($(options.optionList).selection) ]).html());
            $(options.optionList[ $(this).index($(options.optionList).selection) ]).click();

			// hiding the list.
            $(options.uiChild).hide();
			$(options.selectInstance).trigger("change");
            
            options.callfun(); 
            
        });
        
		// When then label has clicked.
        $(options.uiContainer).children('.jqselectlabel').click(function() {
			// Listing the select options.
            $(options.uiChild).slideToggle();
        
        });
        
        
        });
        if ( options.selectfirst == true ){
            $(options.uiItems).each(function(){
                if ( $(this).find("a").html() == $(options.selectInstance).find('option:selected').html() ){
                   $(options.uiContainer).children('.jqselectlabel').html( $(this).find("a").html() );
                }
            });
        }
    });
	
	// Begin the plugin resposable of move the product list when the limit has passed.
	$.fn.PukisImageSlider = (function (options) {

	var options = $.extend({
		selectInstance : $(this),
		enableKeyboard : false,
		autoSlide : false,
		autoSlideInterval : 0,
		autoSlidePause : 0,
		autoSlidePauseVideo : 0,
		slideDuration : 1000
	}, options);

	var slideLength = $(options.selectInstance).find("li").width(); 		// Distância a ser percorrida na animação
	
	//Removendo todos os LIs vazios
	$(options.selectInstance).find("li:empty").remove();
	
	// Modificando a largura nominal do slider (CSS) para o literal
	var sliderTotalWidth = slideLength * $(options.selectInstance).find("li").length;	
	$(options.selectInstance).css("width", sliderTotalWidth);
	
	// Adicionando Botões
	$(options.selectInstance).before('<a class="JQPrevButton" href="javascript:void(0)">Prev</a>');
	$(options.selectInstance).after('<a class="JQNextButton" href="javascript:void(0)">Next</a>');
	
	$(options.selectInstance).find("li:first").addClass("onStage").css("visibility", "visible");		 	// Adiciona classe "visible" no primeiro elemento
	
	$(options.selectInstance).parent().find(".JQNextButton").click(function(){  // Ao clicar no botão da direita (Próximo)
		moveRight();
	});
	$(options.selectInstance).parent().find(".JQPrevButton").click(function(){  // Ao clicar no botão da esquerda (Anterior)
		moveLeft();
	}); 
	
	
	// Redimensionando o slider para o tamanho da janela ao carregar
	// Redimensionando o slider para o tamanho da janela ao redimensionar
	resizeSlider();
	window.setTimeout(function(){ resizeSlider(); }, 5000);
	window.onresize = resizeSlider;		
	
	// Caso a opção enableKeyboard esteja habilitada, adiciona dois eventos as setas do teclado
	if ( options.enableKeyboard ) {
		$(document).keydown(function (event) {
			if (event.keyCode == 39)	 { 
				moveRight();
			} 
			else if (event.keyCode == 37){ 
				moveLeft();
			}
		});
	}
	
	if(options.autoSlide){
		options.autoSlideInterval += options.slideDuration;
		var slideAutomatic = window.setInterval(function(){ moveRight(true); }, options.autoSlideInterval);
		var slidePause;
		$(options.selectInstance).parent().find(".JQNextButton, .JQPrevButton").click(function(){
			clearInterval(slideAutomatic);
			clearTimeout(slidePause);
			slidePause = setTimeout(function(){
				slideAutomatic = window.setInterval(function(){ moveRight(true); }, options.autoSlideInterval);
			}, options.autoSlidePause);
					});
					if ( options.enableKeyboard ) {
			$(document).keydown(function (event) {
				if (event.keyCode == 39 || event.keyCode == 37){ 
					clearInterval(slideAutomatic);
					clearTimeout(slidePause);
					slidePause = setTimeout(function(){
						slideAutomatic = window.setInterval(function(){ moveRight(true); }, options.autoSlideInterval);
					}, options.autoSlidePause);
				}
			});
		}
		$(options.selectInstance).find(".samba-player object").live('mousedown', function(){
			$(".samba-player").css("background", "");
			clearInterval(slideAutomatic);
			clearTimeout(slidePause);
		});
		
	}
	
	// Função para o slider ir para a esquerda
	function moveLeft(){
		if(!$(options.selectInstance).find("li").is(":animated")){
			var slideFrom = $(options.selectInstance).find("li.onStage");
			var slideTo = $(options.selectInstance).find("li.onStage").prev();

			if(slideTo.length === 0){ slideTo = $(options.selectInstance).find("li:last"); }

			slideTo.css("left", slideLength * (-1));

			slideFrom.animate({ left : slideLength }, options.slideDuration,function(){ 
				$(this).removeClass("onStage").css("visibility", "hidden"); 
			});

			slideTo.css("visibility", "visible").animate({ left : 0 }, options.slideDuration, function(){ 
				$(this).addClass("onStage");
			});
		}
	}
	
	// Função para o slider ir para a direita
	function moveRight(auto){
		if(!$(options.selectInstance).find("li").is(":animated")){
		
			var slideFrom = $(options.selectInstance).find("li.onStage");
			var slideTo = $(options.selectInstance).find("li.onStage").next();
			
			if(slideFrom.find("object").length == 1){
				var video = slideFrom.find("object").parent().html();
				slideFrom.empty().append(video);
			}
			
			if(slideTo.length === 0){ slideTo = $(options.selectInstance).find("li:first"); }

			slideTo.css("left", slideLength);

			slideFrom.animate({ left : slideLength * (-1) }, options.slideDuration,function(){
				$(this).removeClass("onStage").css("visibility", "hidden");
			});

			slideTo.css("visibility", "visible").animate({ left : 0 }, options.slideDuration, function(){
				$(this).addClass("onStage");
				
				if(auto === true && options.autoSlide && $(".onStage").find("img").length == 0){
					clearInterval(slideAutomatic);
					clearTimeout(slidePause);
					slidePause = setTimeout(function(){
						moveRight(true);
						slideAutomatic = window.setInterval(function(){ moveRight(true); }, options.autoSlideInterval);
					}, options.autoSlidePauseVideo);
				}
			});
		}
	}
	
	// Função de redimensionamento do slider
	function resizeSlider() {
		var newWidth = $(window).width();
		var newHeight = $(window).height();
		
		$(options.selectInstance).find("li").each(function(){
			$(this).find("img, object, iframe").removeAttr("width");
			$(this).find("img, object, iframe").removeAttr("height");

			var elementWidth = $(this).find("img, object, iframe").width();
			var elementHeight = $(this).find("img, object, iframe").height();

			// O algoritmo não faz a escala, as imagens precisam estar em tamanho full
			if($(this).find("img").length == 1){
				$(this).find("img").attr("width", newWidth);
				$(this).find("img").attr("height", ( newWidth / ( elementWidth / elementHeight ) ));
				$(this).css("width", newWidth);
			}else if($(this).find("iframe").length == 1 || $(this).find("object").length == 1 ){
				$(this).find("iframe").attr("src", $(this).find("iframe").attr("src") + "?wmode=Transparent");
				$(this).find("iframe, object, embed").attr("width", newWidth - 100);
				$(this).find("iframe, object, embed").attr("height", newHeight - 20);
				$(this).css("width", newWidth);
				$(this).css("background", "url('/Control/ArquivoExibir.aspx?NomeArquivo=slider-loading.gif') no-repeat center " + newHeight/2 + "px" );
			}
		});		
		slideLength = newWidth; // Modificando a distância a ser perocrrida pelo slider
		$(options.selectInstance).parent().find(".JQPrevButton, .JQNextButton").css("height", newHeight);
	}
});
	
	// Begin the plugin resposable of move the product list when the limit has passed.
	$.fn.jQRoll = (function (options) {
		// Defining all customs variables.
        var options = $.extend({
            selectInstance : $(this),
            selectList : $(this).find('li:has("img,a,div,swf,embed,script")'),
            selectListSize : $(this).find('li:has("img,a,div,swf,embed,script")').length,
			containerInstance : null,
			containerLimit : 1,
			moveSize : $(this).find('li:first-child').width(),
			prevButton :  null,
			nextButton :  null,
			track : null,
			enableKeyboard: false
	    },options);

		$(this).find('li:not(:has("img,a,div,swf,embed,script"))').remove();
		
		options.track = 0;
		var boxindex = 0;
		
		// appending a container to the mathed ul.
		$(options.selectInstance).before('<div id="JQRollInstance"> </div>');
		// Matching the added container to a variable.
		options.containerInstance = $(options.selectInstance).prev();
		// Appending the mathed ul on the container.
		$(options.containerInstance).append(options.selectInstance);
		// Appeding the previously button.
		$(options.containerInstance).before('<a class="JQPrevButton" href="javascript:void(0)">Prev</a>');
		// Appeding the next button
		$(options.containerInstance).after('<a class="JQNextButton disabled" href="javascript:void(0)">Next</a>');
		// Finding the prev button.
		options.prevButton = $(options.containerInstance).parent().find(".JQPrevButton");
		// Finding the next button.
		options.nextButton = $(options.containerInstance).parent().find(".JQNextButton");
		
		if ( options.selectListSize <= options.containerLimit )
		{
		
			$(options.prevButton).addClass("disabled");
		
		}

		// When the prev buttton has clicked.
		$(options.prevButton).click(function () {
			
			if( ( boxindex + options.containerLimit ) + 1 <= options.selectListSize ) {
			
				options.track -= options.moveSize;
				boxindex += 1;
				
				
				$(options.selectInstance).animate({ left : options.track }, 500);
				
				if( ( boxindex + options.containerLimit ) + 1 <= options.selectListSize ) {
					
					$(options.prevButton).removeClass('disabled');
					
				}else{
					
					$(options.prevButton).addClass('disabled');
				
				}
				
				if( options.track < 0 ) {
				
					$(options.nextButton).removeClass('disabled');
					
				}else{
					
					$(options.nextButton).addClass('disabled');
				
				}
				
			}
			
		});
		
		// When the next button has clicked.
		$(options.nextButton).click(function () {
		
			//if( options.track < ( options.moveSize * options.selectListSize )  && options.track < ( options.moveSize * options.containerLimit )  ) {
			if( options.track < 0 ) {
				
				options.track += options.moveSize;
				boxindex -= 1;
				
				$(options.selectInstance).animate({ left : options.track }, 500);
			
				if( options.track < 0 ) {
				
					$(options.nextButton).removeClass('disabled');
				
				}else{
					
					$(options.nextButton).addClass('disabled');
					
				}
				
				if( ( boxindex + options.containerLimit ) + 1 <= options.selectListSize ) {
				
					$(options.prevButton).removeClass('disabled');
					
				}else{
					
					$(options.prevButton).addClass('disabled');
				
				}
				
			}

		
		});
		
		$(document).keydown(function (event) {
			
			if ( options.enableKeyboard ) {
			
				// handle cursor keys
				if (event.keyCode == 39) {
					// go left
					if( ( boxindex + options.containerLimit ) + 1 <= options.selectListSize ) {
					
						options.track -= options.moveSize;
						boxindex += 1;
						
						
						$(options.selectInstance).animate({ left : options.track }, 500);
						
						if( ( boxindex + options.containerLimit ) + 1 <= options.selectListSize ) {
							
							$(options.prevButton).removeClass('disabled');
							
						}else{
							
							$(options.prevButton).addClass('disabled');
						
						}
						
						if( options.track < 0 ) {
						
							$(options.nextButton).removeClass('disabled');
							
						}else{
							
							$(options.nextButton).addClass('disabled');
						
						}
						
					}
					
				} else if (event.keyCode == 37) {
					// go right
					
					if( options.track < 0 ) {
						
						options.track += options.moveSize;
						boxindex -= 1;
						
						$(options.selectInstance).animate({ left : options.track }, 500);
					
						if( options.track < 0 ) {
						
							$(options.nextButton).removeClass('disabled');
						
						}else{
							
							$(options.nextButton).addClass('disabled');
							
						}
						
						if( ( boxindex + options.containerLimit ) + 1 <= options.selectListSize ) {
						
							$(options.prevButton).removeClass('disabled');
							
						} else {
							
							$(options.prevButton).addClass('disabled');
						
						}
						
					}
					
				}
				
			}
			
		});
		
	});
	
	// Begin the plugin resposable of move the product list when the limit has passed.
	$.fn.DasluProductSlider = (function (options) {
		
		$(this).find('li:gt(5)').remove();
	
		// Defining all customs variables.
        var options = $.extend({
            selectInstance : $(this),
            selectList : $(this).find('li'),
            selectListSize : $(this).find('li').length,
			containerInstance : null,
			prevButton :  null,
			nextButton :  null,
			track : null,
			enableKeyboard: false
	    },options);
		
 		// appending a container to the mathed ul.
		$(options.selectInstance).before('<div id="JQRollInstance"> </div>');
		// Matching the added container to a variable.
		options.containerInstance = $(options.selectInstance).prev();
		// Appending the mathed ul on the container.
		$(options.containerInstance).append(options.selectInstance);
		// Appeding the previously button.
		$(options.containerInstance).before('<a class="JQPrevButton disabled" href="javascript:void(0)">Prev</a>');
		// Appeding the next button
		$(options.containerInstance).after('<a class="JQNextButton" href="javascript:void(0)">Next</a>');
		// Finding the prev button.
		options.prevButton = $(options.containerInstance).parent().find(".JQPrevButton");
		// Finding the next button.
		options.nextButton = $(options.containerInstance).parent().find(".JQNextButton");

		if ( options.selectListSize < 5 ) {
			$(options.prevButton).addClass('disabled');
			$(options.nextButton).addClass('disabled');
		} 
		
		$(options.selectInstance).css({ 'left' : '0px' });

		// When the prev buttton has clicked.
		$(options.prevButton).click(function () {

			switch(options.selectListSize) {
				case 5:
					$(options.selectInstance).animate({ left : 0 }, 500);
					$(options.prevButton).addClass('disabled');
					$(options.nextButton).removeClass('disabled');
					break;
				case 6:
					$(options.selectInstance).animate({ left : 0 }, 500);
					$(options.prevButton).addClass('disabled');
					$(options.nextButton).removeClass('disabled');
					break;	
				default:
					$(options.selectInstance).animate({ left : 0 }, 500);
					$(options.prevButton).addClass('disabled');
					$(options.nextButton).addClass('disabled');
				break;
			}
			
		});
		
		// When the next button has clicked.
		$(options.nextButton).click(function () {
			switch(options.selectListSize) {
				case 5:
					$(options.selectInstance).animate({ left : -120 }, 500);
					$(options.nextButton).addClass('disabled');
					$(options.prevButton).removeClass('disabled');
					break;
				case 6:
					$(options.selectInstance).animate({ left : -240 }, 500);
					$(options.nextButton).addClass('disabled');
					$(options.prevButton).removeClass('disabled');
					break;	
				default:
					$(options.selectInstance).animate({ left : 0 }, 500);
					$(options.prevButton).addClass('disabled');
					$(options.nextButton).addClass('disabled');
				break;
			}
			
		});
		
	});
	
	$.fn.VerticalSlide = (function (options,diference) {
	
		$(this).find(" > li").each(function(){
			if($(this).text() == ""){
				$(this).remove();
			}
		});
	
        var options = $.extend({
        
            
            boxes : $(this).find(' > li').length,
            boxesHeight : $(this).find('li').height() + diference,
            containerSupport : 3,
            containerSize : $(this).find('li').length * $(this).find('li').height() + 150,
            prev : $('.topcontrols .controlup'),
            next : $('.bottomcontrols .controldown'),
            track: $(this)
       
        
        },options);
		
		
        
        //Verifying if the page want the plugin
        if ( options.boxes <= options.containerSupport ) return false; 
        
        
        // Variable resposable of alocate the slide index
        var boxIndex = 1
        var track = 0
        
        // Fixing the ul width;
        $(options.track).height(options.containerSize);
        
        // Function resposable of move the roll to the next step.
        function gotoNext() {
		
            if ( ( boxIndex + options.containerSupport ) <= options.boxes  ) {
                
                track -=  options.boxesHeight;
                
                // Decreasing the top anchor of the roll
                $(options.track).animate({ top :  track }, 500);
                // Increasing the index
                boxIndex++;
            
            }
        
        }
        
        // Function resposable of move the roll to the previously step.
        function gotoPrev() {
            
            if ( boxIndex - 1 > 0 ) {
              
                track +=  options.boxesHeight;
                // Decreasing the left anchor of the roll
                $(options.track).animate({ top : track }, 500);
                // Increasing the index
                boxIndex--;
            
            }
            
        }

        // Prev Button Click Event
        $(options.prev).click(function() {
            gotoPrev();
       
        });
        
        // Next Button click Event
        $(options.next).click(function() {
            gotoNext();
        
        });
    
    });
	
});